package com.gitee.fsmxm.thinking.in.spring.dependency.injection;

import com.gitee.fsmxm.thinking.in.spring.dependency.domain.UserHolder;
import com.gitee.fsmxm.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * 注解方式 依赖注入 demo
 */
public class AnnotationDependencySetterInjectionDemo {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();

        applicationContext.register(AnnotationDependencySetterInjectionDemo.class);

        BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(applicationContext);

        beanDefinitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");

        applicationContext.refresh();

        System.out.println(applicationContext.getBean(UserHolder.class));

    }

    @Bean
    public UserHolder userHolder(User user) {
        // or new and use setter
        return new UserHolder(user);
    }

}
